// pages/zeroBuy/index/index.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        type: null,
        zeroBuy: [],
        zeroBuyList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // if (options.storeId) {
        //     wx.setStorageSync('storeId', options.storeId)
        //     if (wx.getStorageSync('token')) {
        //         let data = {
        //             store_id: options.storeId
        //         }
        //         common.post("Store/change_user_store", data).then(function(res) {}).catch(function(e) {})
        //     }
        // }
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        this.setData({
            nowDate: new Date().getTime(),
            type: options.type
        })
        this.getZeroBuyList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        const that = this
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        return {
            title: '邀好友助力，看看我的人品好不好？',
            path: '/pages/zeroBuy/index/index?userShare=' + userId,
            imageUrl: 'https://yidianyuan.bailingyoupin.com/static/images/wx_sm/home/shop-zero-buy-relay.jpg'
        }
    },

    getZeroBuyList: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            activity: 785,
            type: 6
        }
        common.post('Shelf/get_shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    zeroBuy: []
                })
            } else {
                let zeroBuyList = that.data.zeroBuyList
                that.setData({
                    zeroBuy: res
                })
                that.data.zeroBuy.forEach((item, i) => {
                    let data = {
                        activity: 785,
                        shelf_id: item.id
                    }
                    common.post('Activity/get_activity_goods', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            zeroBuyList[i] = []
                        } else {
                            zeroBuyList[i] = res
                        }
                        that.setData({
                            zeroBuyList: zeroBuyList
                        })
                    }).catch(function(e) {})
                })
            }
        }).catch(function(e) {})
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/zeroBuy/detail/detail?goodsId=' + e.currentTarget.dataset.id + '&shelfId=' + e.currentTarget.dataset.shelf
        })
    }
})